/**
 * Copyright (c) 1992-2012
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: OracleDialect.java 2012-06-07 Ashton.J $
 */
package org.ashton.mybatis.dialect;

/**
 * Oracle数据库方言<p>
 * 
 * @author  Ashton.J
 * @version v1.0
 */
public class OracleDialect implements Dialect {
    
    public String getLimitString(String origSql, int offset, int limit) {

        boolean isForUpdate = false;
        if (origSql.toLowerCase().endsWith(" FOR UPDATE")) {
            origSql = origSql.substring(0, origSql.length() - 11);
            isForUpdate = true;
        }

        StringBuffer pagingSql = new StringBuffer(origSql.length() + 100);
        pagingSql.append("SELECT * FROM ( SELECT ROW_.*, ROWNUM ROWNUM_ FROM ( ");
        pagingSql.append(origSql);
        pagingSql.append(" ) ROW_ ) WHERE ROWNUM_ > " + offset + " AND ROWNUM_ <= " + (offset + limit));

        if (isForUpdate) {
            pagingSql.append(" FOR UPDATE");
        }

        return pagingSql.toString();
    }
}
